package com.jdbcTest;

import java.sql.*;

public class TestJdbc {
    public static void main(String[] args) throws ClassNotFoundException, SQLException {

        // 设置连接的url /test 是数据库名字 后面的一串是时区和编码
        String url="jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=UTF-8&userSSL=false&serverTimezone=GMT%2B8";
        // 数据库用户
        String username = "root";
        // 密码
        String password = "swj020501";


        // 1.加载驱动
        Class.forName("com.mysql.cj.jdbc.Driver");
        // 2.连接数据库
        Connection connection = DriverManager.getConnection(url,username,password);
        // 3.想数据库发送SQL的对象Statement : CRUD
        Statement statement = connection.createStatement();
        // 4.编写SQL
        String sql = "select* from user";


        ResultSet resultSet = statement.executeQuery(sql);

        while (resultSet.next()){
            System.out.print("id="+resultSet.getObject("id")+" ");
            System.out.print("name="+resultSet.getObject("name")+" ");
            System.out.println("age="+resultSet.getObject("age")+" ");
            System.out.println("sex="+resultSet.getObject("sex")+" ");
        }
    }
}
